using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace MoEngine
{
    public class Button : Window
    {
        Texture2D ButtonImage;
        Vector2Int ImageSize;

        public enum ButtonStates
        {
            Normal,
            Focused,
            Pressed,
            Disabled,
            Max
        }

        private ButtonStates m_ButtonState;

        public ButtonStates ButtonState
        {
            get { return m_ButtonState; }
            protected set { m_ButtonState = value; }
        }

        public Button(WindowBase parent, Texture2D buttonImage)
            : base(parent)
        {
            m_bTrackMouseEnterExit = true;
            ButtonImage = buttonImage;
            ImageSize = new Vector2Int(buttonImage.Width, buttonImage.Height / (int)ButtonStates.Max);
        }

        public override void OnEnter(InputEventArgs evtArgs)
        {
            base.OnEnter(evtArgs);

            // TODO: may not need this, since the button should not receive any input when disabled
            if (m_ButtonState != ButtonStates.Disabled)
            {
                ButtonState = ButtonStates.Focused;
            }
        }

        public override void OnExit(InputEventArgs evtArgs)
        {
            base.OnExit(evtArgs);

            if (m_ButtonState != ButtonStates.Disabled)
            {
                ButtonState = ButtonStates.Normal;
            }
        }

        public override void OnMouseDown(MouseButtons button, Microsoft.Xna.Framework.Point mousePos, InputEventArgs evtArgs)
        {
            base.OnMouseDown(button, mousePos, evtArgs);

            if (m_ButtonState != ButtonStates.Disabled)
            {
                ButtonState = ButtonStates.Pressed;
            }
        }

        public override void OnMouseUp(MouseButtons button, Microsoft.Xna.Framework.Point mousePos, InputEventArgs evtArgs)
        {
            base.OnMouseUp(button, mousePos, evtArgs);

            if (m_ButtonState != ButtonStates.Disabled)
            {
                ButtonState = m_bMouseEntered ? ButtonStates.Focused : ButtonStates.Normal;
            }
        }

        protected Rectangle ImageRect
        {
            get
            {
                return new Rectangle(0, (int)ButtonState * ImageSize.Y, ImageSize.X, ImageSize.Y);
            }
        }

        protected override void DrawWindow(Microsoft.Xna.Framework.GameTime time)
        {
            base.DrawWindow(time);

            BeginDraw();

            m_SpriteBatch.Draw(ButtonImage, DrawRect, ImageRect, Color.White);

            EndDraw();
        }
    }
}
